package com.kede.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kede.dto.AjaxMessage;
import com.kede.dto.LayuiTableData;
import com.kede.entity.Release;
import com.kede.service.MenuService;
import com.kede.service.ReleaseService;
import com.kede.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.ModelAndView;
import java.util.ArrayList;
import java.util.List;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 【请填写作者】
 * @since 2020-04-02
 */
@Controller
@RequestMapping("/skill")
public class ReleaseController {
    @Autowired
    MenuService menuService;
    @Autowired
    ReleaseService releaseService;

    @Autowired
    UserService userService;
    @GetMapping({"/release"})
    public String release(String id,
                          Model model){

        model.addAttribute("mid",id);
        return "skill/release";
    }

    @ResponseBody
    @GetMapping("/releaseList")
    public LayuiTableData releaseList(
            String mid,
            String title,
            @RequestParam (value = "page",defaultValue = "1")Integer page,
            @RequestParam(value = "limit",defaultValue = "10") Integer limit){

        QueryWrapper<Release> queryWrapper = new QueryWrapper<>();
        if (StringUtils.hasLength(title)){
            queryWrapper.like("title",title);
        }else{
            queryWrapper.eq("mid",mid);
        }
        queryWrapper.eq("uid",1);

        Page<Release> rolePage = new Page<>();
        rolePage.setCurrent(page);
        rolePage.setSize(limit);

        Page<Release> releaseRlt = releaseService.page(rolePage,queryWrapper);
        LayuiTableData layuiTableData = new LayuiTableData();
        List<Release> releases = new ArrayList<Release>();
        for (Release release :releaseRlt.getRecords()){
            release.setUsername(userService.getById(release.getUid()).getUsername());
            releases.add(release);
        }
        if(releaseRlt.getRecords() == null){
            layuiTableData.setCount(1L);
            layuiTableData.setMsg("Fail");
            return layuiTableData;
        }else {
            layuiTableData.setCode(0L);
            layuiTableData.setCount(releaseRlt.getTotal());
            layuiTableData.setData(releases);
            return layuiTableData;
        }
    }

//            for (Release release:releaseRlt.getRecords()) {
//            release.setUsername(userService.getById(release.getUid()).getUsername());
//            releases.add(release);
//        }
//        layuiTableData.setData(releases);
//        layuiTableData.setCount(releaseRlt.getTotal());
//        layuiTableData.setCode(0);
//        return layuiTableData;
//    }

    @GetMapping("/releaseEdit/{id}")
    public ModelAndView releaseEdit(@PathVariable("id") Integer id){
        Release release = releaseService.getById(id);
        ModelAndView modelAndView = new ModelAndView("skill/edit");
        modelAndView.addObject("release",release);
        return modelAndView;
    }

    @ResponseBody
    @PostMapping("/releaseUpdate")
    public AjaxMessage releaseUpdate(Release release){
        AjaxMessage ajaxMessage = new AjaxMessage();
        boolean b = releaseService.updateById(release);
        if(b == true){
            ajaxMessage.setCode(0);
            return ajaxMessage;
        }else{
            ajaxMessage.setCode(1);
            ajaxMessage.setMsg("Fail");
            return ajaxMessage;
        }
    }

    @ResponseBody
    @PostMapping("/releaseDelete/{id}")
    public AjaxMessage userDelete(@PathVariable("id") Integer id){
        AjaxMessage ajaxMessage = new AjaxMessage();
        int num = releaseService.deleteReleaseById(id);
        if(num < 0){
            ajaxMessage.setCode(1);
            ajaxMessage.setMsg("Fail");
            return ajaxMessage;
        }else {
            ajaxMessage.setCode(0);
            return ajaxMessage;
        }
    }

    @GetMapping("/rd_add")
    public String userAdd(
            String mid,
            Model model) {
        model.addAttribute("mid",mid);
        model.addAttribute("release",new Release());
        return "skill/add";
    }

//    @GetMapping("/releaseSreach")
//    public LayuiTableData releaseSreach(String title,Integer page, Integer limit){
//        LayuiTableData layuiTableData = new LayuiTableData();
//        List<Release> releaseList = releaseService.releaseSreachByTitle(title,(page-1)*limit,limit);
//        Integer count = releaseService.countByTitle(title);
//        if (releaseList == null){
//            layuiTableData.setCode(1L);
//            layuiTableData.setMsg("Fail");
//            return layuiTableData;
//        }else {
//            layuiTableData.setCode(0L);
//            layuiTableData.setCount(Long.valueOf(count));
//            layuiTableData.setData(releaseList);
//            return layuiTableData;
//        }
//    }


    @ResponseBody
    @PostMapping("re_date")
    public AjaxMessage updatebp(Release release){
        AjaxMessage message = new AjaxMessage();
        long uid =Long.parseLong("1");
        release.setUid(uid);
        if (releaseService.save(release)){
            message.setCode(0);
        }else {
            message.setCode(1);
            message.setMsg("更新失败");
        }
        return message;
    }

}
